﻿
namespace QuizSilverlight.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Linq;
    using System.Linq;
    using System.Web.DomainServices;
    using System.Web.DomainServices.Providers;
    using System.Web.Ria;
    using System.Web.Ria.Services;


    // Implements application logic using the QuizDataClassesDataContext context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class QuizService : LinqToSqlDomainService<QuizDataClassesDataContext>
    {
        // TODO: To change when its needed (stewie)
        private int WebSiteID = 1;
        private static int QuestionID = 0;

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<Answer> GetAnswer()
        {
            return this.DataContext.Answer.Where( a => a.Id_Question ==QuestionID);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<Participant> GetParticipant()
        {
            return this.DataContext.Participant;
        }

        public void InsertParticipant(Participant participant)
        {

            participant.Id_WebSite = WebSiteID;
            this.DataContext.Participant.InsertOnSubmit(participant);
        }

        public void UpdateParticipant(Participant currentParticipant)
        {
            this.DataContext.Participant.Attach(currentParticipant, this.ChangeSet.GetOriginal(currentParticipant));
        }

        public void DeleteParticipant(Participant participant)
        {
            this.DataContext.Participant.Attach(participant);
            this.DataContext.Participant.DeleteOnSubmit(participant);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public Question GetQuestion()
        {
            IQueryable<Question> _questions = DataContext.Question.Where(q => q.Id_WebSite == WebSiteID);
            Random random = new Random();
            int nbQuestion = random.Next(_questions.Count());
            QuestionID = (_questions.ToList())[nbQuestion].Id_Question;
            return (_questions.ToList())[nbQuestion];
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<WebSite> GetWebSite()
        {
            return this.DataContext.WebSite;
        }
    }
}


